package com.cloud.seatarmtwo.service.impl;

import com.cloud.seatarmtwo.dao.TblTwoDao;
import com.cloud.seatarmtwo.entity.TblTwo;
import com.cloud.seatarmtwo.entity.TblTwoExample;
import com.cloud.seatarmtwo.service.Seata_TCC_Service;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class Seata_TCC_ServiceImpl implements Seata_TCC_Service {

    @Autowired
    private TblTwoDao tblTwoDao;

    @Override
    @Transactional
    public String test_TCC(BusinessActionContext businessActionContext) {
        TblTwo two = TblTwo.builder().name("Seata_TCC_测试").build();
        tblTwoDao.insert(two);
        return "测试成功";
    }

    @Override
    @Transactional
    public boolean commitTCC(BusinessActionContext businessActionContext) {
        System.out.println("提交成功");
        return true;
    }

    @Override
    @Transactional
    public boolean rollbackTCC(BusinessActionContext businessActionContext) {
        Map<String,Object> columnsMap = new HashMap<>();
        columnsMap.put("name","Seata_TCC_测试");
        tblTwoDao.deleteByMap(columnsMap);
        System.out.println("回滚成功");
        return true;
    }
}
